package com.organization.model.services.impl

import org.springframework.stereotype.Service
import com.organization.model.services.ProjectService
import com.organization.model.dao.ProjectDao
import com.organization.services.CommonService
import org.springframework.beans.factory.annotation.Autowired
import javax.validation.Validator
import javax.validation.Validation
import com.organization.model.Project
import com.organization.model.Department
import javax.validation.ConstraintViolation
import com.organization.model.services.DepartmentService

/**
 * Created with IntelliJ IDEA.
 * User: inester
 * Date: 10.07.12
 * Time: 13:26
 * To change this template use File | Settings | File Templates.
 */
@Service("projectService")
class ProjectServiceImpl implements ProjectService {

    @Autowired
    ProjectDao projectDao
    @Autowired
    CommonService commonService
    @Autowired
    DepartmentService departmentService

    def getProjectInfo(projId){
        Project proj = get(commonService.nodeIdToDbId(projId))
        if (proj){
            def result = [name:proj.name, startDate:proj.startDate, dueDate:proj.dueDate, description: proj.description]
            result.putAll(commonService.goodResponse)
            return result
        }
        return commonService.badResponse
    }

    def updateProjectInfo(params){
        Project proj = get(commonService.nodeIdToDbId(params.nodeId))
        if (proj){
            proj.name = params.name
            proj.startDate = commonService.convertDateFromJson(params.startDate)
            proj.dueDate = commonService.convertDateFromJson(params.dueDate)
            proj.description = params.description
            //validation
            Set<ConstraintViolation<Project>> constraintViolations = validator.validate(proj);
            if (constraintViolations.size()){
                return commonService.parseConstraintsFromDomain(constraintViolations)
            }
            else{
                update(proj)
                def response = [:]
                response.putAll(commonService.goodResponse)
                response.putAt('newName',params.name)
                response.putAt('nodeId',params.nodeId)
                return response
            }
        }
        return commonService.badResponse
    }

    def addProject(params){
        Department dep = departmentService.get(commonService.nodeIdToDbId(params.parentNodeId))
        if (dep){
            Project proj = new Project(
                    department: dep,
                    name: params.name,
                    startDate: commonService.convertDateFromJson(params.startDate),
                    dueDate: commonService.convertDateFromJson(params.dueDate),
                    description: params.description
            );
            if (proj){
                //validation
                Set<ConstraintViolation<Project>> constraintViolations = validator.validate(proj);
                if (constraintViolations.size()){
                    return commonService.parseConstraintsFromDomain(constraintViolations)
                }
                else{
                    save(proj)
                    def response = [:]
                    response.putAll(commonService.goodResponse)
                    response.putAt('nodeId',commonService.projNodeStr+proj.id.toString())
                    response.putAt('parentNodeId',params.parentNodeId)
                    return response
                }
            }
        }
        return commonService.badResponse
    }

    def deleteProject(params){
        Project proj = get(commonService.nodeIdToDbId(params.nodeId))
        if (proj){
            delete(proj)
            return commonService.goodResponse
        }
        return commonService.badResponse;
    }

    @Override
    public void save(Project proj){
        projectDao.save(proj)
    }

    @Override
    public void update(Project proj){
        projectDao.update(proj)
    }

    @Override
    public void delete(Project proj){
        projectDao.delete(proj)
    }

    @Override
    public List<Project> projectList() {
        projectDao.projectList()
    }

    @Override
    public List<Project> findAllByDepartment(Department dep){
        projectDao.findAllByDepartment(dep)
    }

    @Override
    public Project get(int id) {
        projectDao.get(id)
    }
    //validator
    private static Validator validator = Validation.buildDefaultValidatorFactory().getValidator();

}
